package com.ruoyi.project.jiaqin.quoted.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.quoted.domain.QuotedPrice;
import com.ruoyi.project.jiaqin.quoted.mapper.QuotedPriceMapper;
import com.ruoyi.project.jiaqin.quoted.service.IQuotedPriceService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 采购报价Service业务层处理
 *
 * @author admin
 * @date 2020-12-25
 */
@Service
public class QuotedPriceServiceImpl implements IQuotedPriceService
{
    @Autowired
    private QuotedPriceMapper quotedPriceMapper;

    /**
     * 查询采购报价
     *
     * @param id 采购报价ID
     * @return 采购报价
     */
    @Override
    public QuotedPrice selectQuotedPriceById(String id)
    {
        return quotedPriceMapper.selectQuotedPriceById(id);
    }

    /**
     * 查询采购报价列表
     *
     * @param quotedPrice 采购报价
     * @return 采购报价
     */
    @Override
    public List<QuotedPrice> selectQuotedPriceList(QuotedPrice quotedPrice)
    {
        return quotedPriceMapper.selectQuotedPriceList(quotedPrice);
    }

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IMemberService memberService;
    @Override
    public PageInfo<QuotedPrice> selectQuotedPriceListByWantToBuyId(Integer pageNum ,Integer pageSize, String wantToBuyId)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<QuotedPrice> list  = quotedPriceMapper.selectQuotedPriceListByWantToBuyId(wantToBuyId);
        for (QuotedPrice QP : list) {
            // 是否是企业
            Company company = companyService.selectCompanyByMemberId(QP.getQuotedMemberId());
            Member member = memberService.selectMemberById(QP.getQuotedMemberId());
            if (company != null) {
                QP.setIsCompany(1);
                QP.setMemberCompanyId(company.getId());
            } else {
                QP.setIsCompany(0);
                QP.setMemberCompanyId(member.getId());
            }
        }
        PageInfo<QuotedPrice> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增采购报价
     *
     * @param quotedPrice 采购报价
     * @return 结果
     */
    @Override
    public int insertQuotedPrice(QuotedPrice quotedPrice)
    {
        quotedPrice.setId(UUID.randomUUID().toString().replace("-",""));
        return quotedPriceMapper.insertQuotedPrice(quotedPrice);
    }

    /**
     * 修改采购报价
     *
     * @param quotedPrice 采购报价
     * @return 结果
     */
    @Override
    public int updateQuotedPrice(QuotedPrice quotedPrice)
    {
        return quotedPriceMapper.updateQuotedPrice(quotedPrice);
    }

    /**
     * 删除采购报价对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteQuotedPriceByIds(String ids)
    {
        return quotedPriceMapper.deleteQuotedPriceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除采购报价信息
     *
     * @param id 采购报价ID
     * @return 结果
     */
    @Override
    public int deleteQuotedPriceById(String id)
    {
        return quotedPriceMapper.deleteQuotedPriceById(id);
    }

    @Override
    public QuotedPrice selectQuotedPriceByWantToBuyIdAndMemberId(String memberId, String wantToBuyId) {
        return quotedPriceMapper.selectQuotedPriceByWantToBuyIdAndMemberId(memberId,wantToBuyId);
    }

    @Override
    public QuotedPrice selectQuotedPriceByWantToBuyId(String wantToBuyId, String memberId) {
        return quotedPriceMapper.selectQuotedPriceByWantToBuyId(wantToBuyId, memberId);
    }
}
